/* This is a very simple hello_world program.
 * It aims to demonstrate the co-existence of two processes:
 * One of them prints a hello world message and the other blinks the LEDs
 *
 * It is largely based on hello_world in $(CONTIKI)/examples/sensinode
 *
 * Author: George Oikonomou - <oikonomou@users.sourceforge.net>
 */

#include "contiki.h"
#include "dev/leds.h"
#include "dev/uart0.h"
#include "lib/ringbuf.h"

#include <stdio.h> /* For printf() */

#define BUFSIZE 128

static struct ringbuf rxbuf;
static uint8_t rxbuf_data[BUFSIZE];
static struct etimer et_uart;

int uart_cb(unsigned char c)
{
	leds_toggle(LEDS_GREEN);
	ringbuf_put(&rxbuf, c);
	return 0;
}

/*---------------------------------------------------------------------------*/
PROCESS(uart_process, "uart process");
AUTOSTART_PROCESSES(&uart_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(uart_process, ev, data)
{
	int c;
	PROCESS_BEGIN();
	ringbuf_init(&rxbuf, rxbuf_data, sizeof(rxbuf_data));
	uart0_set_input(uart_cb);
	etimer_set(&et_uart, CLOCK_SECOND / 16);
	while(1)
	{
		PROCESS_WAIT_EVENT();

		if(ev == PROCESS_EVENT_TIMER) {
			while((c = ringbuf_get(&rxbuf)) != -1)
				printf("%c", c);
		  etimer_reset(&et_uart);
		}
	}
	PROCESS_END();
}
/*---------------------------------------------------------------------------*/

